import unittest
from common.base import get_driver
from page.shop_page import ShopPage
from page.product1_page import Product1Page
from page.product2_page import Product2Page
from page.index_page import IndexPage


class ShopTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        driver = get_driver()
        driver.get('http://localhost:8080/ecshop/index.php')
        cls.index = IndexPage(driver)
        cls.product1 = Product1Page(driver)
        cls.product2 = Product2Page(driver)
        cls.shop = ShopPage(driver)

    # 增加商品(智能相机与平衡车)
    def test_01_add(self):
        self.index.click_shop1()
        self.product1.click_add()
        self.shop.index_shop()
        self.index.click_shop2()
        self.product2.click_add()

        # 通过购物车内商品数量断言
        shop_cars = self.shop.shop_text()
        self.assertEqual('购物车(2)', shop_cars, msg='添加失败')

    # 修改商品数量
    def test_02_alter(self):
        num1 = 3
        self.shop.alter_shop(num1)
        self.shop.update_shop()

        # 通过购物车内商品数量断言
        num = int(self.shop.shop_num())
        self.assertEqual(num1, num, msg='数量不一致')

    # 删除商品
    def test_03_delete(self):
        self.shop.delete_shop()

        # 通过购物车内商品数量断言
        shop_cars = self.shop.shop_text()
        self.assertEqual('购物车(3)', shop_cars, msg='删除失败')

    def test_04_clear(self):
        self.shop.clear_shop()
        self.shop.check_shop()

        # 通过购物车内商品数量断言
        shop_cars = self.shop.shop_text()
        self.assertEqual('购物车(0)', shop_cars, msg='清空失败')

    @classmethod
    def tearDownClass(cls) -> None:
        cls.shop.quit(5)


if __name__ == '__main__':
    unittest.main(verbosity=2)
